package de.distmlp.preprocessing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import de.distmlp.preprocessing.data.MLDataEntry;
import de.distmlp.preprocessing.parser.IParser;

public class MLDataProcessor {

	public static List<MLDataEntry> process(final String fileName, final IParser parser, final int nbTrainingsData) throws IOException {
		final File f = new File(fileName);
		final List<MLDataEntry> list = new ArrayList<MLDataEntry>();
		if (f.exists()) {
			final BufferedReader reader = new BufferedReader(new FileReader(f));
			String line;
			while ((line = reader.readLine()) != null) {
				if (list.size() == nbTrainingsData) {
					break;
				}
				list.addAll(parser.parse(line));
			}
			reader.close();
			System.out.println("Parsed " + list.size() + " lines.");
		} else {
			System.out.println("File " + fileName + " does not exist!");
		}
		return list;
	}

	public static void convertMLDataSetToDouble(final List<MLDataEntry> list, final double[][] featureArray, final double[][] targetArray) {
		if ((list == null) || (list.size() == 0)) {
			return;
		}
		for (int i = 0; i < list.size(); i++) {
			featureArray[i] = list.get(i).getInput();
			targetArray[i] = list.get(i).getOutput();
		}
	}
}
